package org.sys.filter;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RequestFilter implements Filter {

	private static Logger log = LoggerFactory.getLogger(RequestFilter.class);

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		log.debug(httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName() + ":"
				+ httpServletRequest.getServerPort() + httpServletRequest.getContextPath()
				+ httpServletRequest.getRequestURI());
		
		String ip = httpServletRequest.getRemoteAddr();
		log.debug("ip = {}", ip);
		
		Enumeration<String> en = httpServletRequest.getHeaderNames();
		
		if(en.hasMoreElements()){
			String value = httpServletRequest.getHeader(en.nextElement());
			log.debug("element = [{}],[{}]",en.nextElement(),value);
		}
		chain.doFilter(request, response);
	}

}
